package name.matthewgreet.example3.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import name.matthewgreet.example3.common.AbstractFormDrivenActionSupport; import name.matthewgreet.example3.common.BrowserTabSession; import name.matthewgreet.example3.common.ListCache; import name.matthewgreet.example3.exception.CountryCodeAlreadyExistsRuleException; import name.matthewgreet.example3.form.CountryForm; import name.matthewgreet.example3.vo.CountryList; import name.matthewgreet.example3.vo.CountryDTO; /** * Adds a new country. */ @InterceptorRef("FormDrivenStack") @Results({ @Result(name=ActionSupport.INPUT, type="redirectAction", params = {"namespace", "/", "actionName", "ViewNewCountry"}), @Result(name=ActionSupport.SUCCESS, type="redirectAction", params = {"namespace", "/", "actionName", "ViewCountryList"}) }) public class AddCountryAction extends AbstractFormDrivenActionSupport<CountryForm> { private static final long serialVersionUID = -700627736127116372L; public AddCountryAction() { super(); } @Action("/AddCountry") @Override public String execute() throws Exception { CountryDTO country; BrowserTabSession browserTabSession; ListCache<Integer,CountryDTO> listCache; try { browserTabSession = getBrowserTabSession(); listCache = ViewCountryListAction.retrieveListCache(browserTabSession); country = new CountryDTO(); country.setCode(getModel().getCode()); country.setName(getModel().getName()); country.setNotes(getModel().getNotes()); country = CountryList.getInstance(browserTabSession).addCountry(country); listCache.addItem(country); addActionMessage(country.getName() + " created"); return ActionSupport.SUCCESS; } catch (CountryCodeAlreadyExistsRuleException e) { addActionError("Country code " + e.getCode() + " already in use"); return ActionSupport.INPUT; } } @Override protected CountryForm makeForm() { return new CountryForm(); } }